//
//  UIAlertController+Extend.m
//  AiTouYun
//
//  Created by 1 on 16/9/18.
//  Copyright © 2016年 junjie.liu. All rights reserved.
//

#import "UIAlertController+Extend.h"



#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
typedef void (^confirm)();
typedef void (^cancle)();

@interface UIAlertController(){
    confirm confirmParam;
    cancle  cancleParam;
}

@end
@implementation UIAlertController (Extend)

+ (UIAlertController *)showAlertView:(UIViewController *)viewController
                               title:(NSString *)title
                             message:(NSString *)message
                   cancelButtonTitle:(NSString *)cancelButtonTitle
                     confirmBtnTitle:(NSString *)confirmBtnTitle
                         cancleBlock:(void (^)())cancle
                        confirmBlock:(void (^)())confirm {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:confirmBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        confirm();
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        cancle();
    }];
    
    if (cancelButtonTitle.length != 0 &&  confirmBtnTitle.length != 0) {
        [alertController addAction:otherAction];
        [alertController addAction:cancelAction];
    } else if (cancelButtonTitle.length != 0 && confirmBtnTitle.length == 0) {
        [alertController addAction:cancelAction];
    } else if (cancelButtonTitle.length == 0 && confirmBtnTitle.length != 0) {
        [alertController addAction:otherAction];
    } else if (cancelButtonTitle.length==0 && confirmBtnTitle.length == 0) {
        alertController = [UIAlertController alertControllerWithTitle:@"这是Bug,请最少填一个选项" message:message preferredStyle:UIAlertControllerStyleAlert];
    }
    
    [viewController presentViewController:alertController animated:YES completion:nil];
    
    return alertController;
}



+ (UIAlertController *)showTextLeftAlertView:(UIViewController *)viewController
                                       title:(NSString *)title
                                     message:(NSString *)message
                           cancelButtonTitle:(NSString *)cancelButtonTitle
                             confirmBtnTitle:(NSString *)confirmBtnTitle
                                 cancleBlock:(void (^)())cancle
                                confirmBlock:(void (^)())confirm {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    
    
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:confirmBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        confirm();
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        cancle();
    }];
    
    if (cancelButtonTitle.length != 0 &&  confirmBtnTitle.length != 0) {
        [alertController addAction:otherAction];
        [alertController addAction:cancelAction];
    } else if (cancelButtonTitle.length != 0 && confirmBtnTitle.length == 0) {
        [alertController addAction:cancelAction];
    } else if (cancelButtonTitle.length == 0 && confirmBtnTitle.length != 0) {
        [alertController addAction:otherAction];
    } else if (cancelButtonTitle.length==0 && confirmBtnTitle.length == 0) {
        alertController = [UIAlertController alertControllerWithTitle:@"这是Bug,请最少填一个选项" message:message preferredStyle:UIAlertControllerStyleAlert];
    }
    
    [viewController presentViewController:alertController animated:YES completion:nil];
    
    
    
    UIView *subView1 = alertController.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];
    
    NSLog(@"%@",subView5.subviews);
    //取title和message：
//    UILabel *title_textLabel = subView5.subviews[0];
    UILabel *message_textLabel = subView5.subviews[1];
    
    message_textLabel.textAlignment = NSTextAlignmentLeft;
    
    
    return alertController;
}




@end
